﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;
using System.Drawing;
using System.Collections.Generic;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.Manager
{
    public partial class OrderManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvOrderManager.DataSource = Coupon_BUS.GetAllOrderCoupon();
                gvOrderManager.DataBind();
            }
        }

        protected void gvOrderManager_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbStatus = (Label)e.Row.FindControl("lbstatus");
                Label lbinformation = (Label)e.Row.FindControl("lbinformation");
                //LinkButton lbselectOrdercoupon = (LinkButton)e.Row.FindControl("lbSelectOrderCoupon");
                if (lbStatus.Text == "1")
                {
                    //lbselectcoupon.Visible = true;
                    lbinformation.Text = "đã chấp nhận";
                }
                else
                {
                    //lbselectcoupon.Visible = false;
                    lbinformation.Text = "chưa chấp nhận";
                }

                Label lbTransportStatus = (Label)e.Row.FindControl("lbTransportStatus");
                Label lbTransportStatusInfo = (Label)e.Row.FindControl("lbTransportStatusInfo");
                if (lbTransportStatus.Text == "1")
                {
                    lbTransportStatusInfo.Text = "đang vận chuyển";
                }
                else
                {
                    lbTransportStatusInfo.Text = "chưa vận chuyển";
                }

                Label lbPaymentStatus = (Label)e.Row.FindControl("lbPaymentStatus");
                Label lbPaymentStatusInfo = (Label)e.Row.FindControl("lbPaymentStatusInfo");
                if (lbPaymentStatus.Text == "1")
                {
                    lbPaymentStatusInfo.Text = "Đã thanh toán";
                }
                else
                {
                    lbPaymentStatusInfo.Text = "chưa thanh toán";
                }
            }
        }

        protected void gvOrderManager_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CheckAll")
            {
                CheckBox cbcheckall = (CheckBox)gvOrderManager.HeaderRow.FindControl("cbCheckAll");
                if (cbcheckall.Checked == true)
                {
                    for (int i = 0; i < gvOrderManager.Rows.Count; i++)
                    {
                        CheckBox cbcheck = (CheckBox)gvOrderManager.Rows[i].FindControl("cbcheck");
                        cbcheck.Checked = false;
                        cbcheckall.Checked = false;
                    }
                }
                else
                {
                    for (int i = 0; i < gvOrderManager.Rows.Count; i++)
                    {
                        CheckBox cbcheck = (CheckBox)gvOrderManager.Rows[i].FindControl("cbcheck");
                        cbcheck.Checked = true;
                        cbcheckall.Checked = true;
                    }
                }
            }
            if (e.CommandName == "btViewOrderDetail")
            {
                string orderCouponID = e.CommandArgument.ToString();
                string OrderDate = "";
                decimal TotalPrice = 0;

                List<ORDER_COUPON_DETAIL> listorderbycompany = Customer_BUS.GetDetailBillPaiedBuyByCompany(orderCouponID, ref OrderDate);
                lbdate.Text = OrderDate;


                gvdetail.DataSource = listorderbycompany;
                gvdetail.DataBind();
                ViewDetailPanel.Visible = true;
                for (int i = 0; i < listorderbycompany.Count; i++)
                {
                    TotalPrice += (decimal)listorderbycompany[i].Cost;
                }
                lbtotalprice.Text = TotalPrice.ToString("###,###,###") + " VND";
            }
        }

        protected void btactive_Click(object sender, EventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < gvOrderManager.Rows.Count; i++)
            {
                CheckBox cbcheck = (CheckBox)gvOrderManager.Rows[i].FindControl("cbcheck");
                if (cbcheck.Checked == true)
                {
                    Label status = (Label)gvOrderManager.Rows[i].FindControl("lbstatus");
                    string orderCouponID = gvOrderManager.Rows[i].Cells[0].Text;
                    if (status.Text == "0")
                    {
                        bool kq = Coupon_BUS.UpdateStatusColumnOrderCoupon(orderCouponID, 1);
                        if (kq == false)
                        {
                            flag = 1;
                            LbResult.Text = "Không Thành Công";
                            break;
                        }
                        else
                        {
                            //goi toi webservice ben mua chung
                            bool callWSResult = CouponWebservice_BUS.CallWebserviceFromMuaChung(orderCouponID);
                            if (callWSResult == true)
                            {
                                LbResult.Text = "ConfirmCoupon thành công";
                                LbResult.ForeColor = Color.Blue;
                            }
                            else
                            {
                                flag = 1;
                                LbResult.Text = "ConfirmCoupon thất bại";
                                LbResult.ForeColor = Color.Red;
                                break;
                            }
                        }
                    }
                   
                }
            }
            if (flag == 0)
            {
                //LbResult.Text = "Thành Công";
                //LbResult.ForeColor = Color.Blue;
                gvOrderManager.DataSource = Coupon_BUS.GetAllOrderCoupon();
                gvOrderManager.DataBind();
            }
            
        }

        protected void btdisactive_Click(object sender, EventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < gvOrderManager.Rows.Count; i++)
            {
                CheckBox cbcheck = (CheckBox)gvOrderManager.Rows[i].FindControl("cbcheck");
                if (cbcheck.Checked == true)
                {
                    Label status = (Label)gvOrderManager.Rows[i].FindControl("lbstatus");
                    string orderCouponID = gvOrderManager.Rows[i].Cells[0].Text;
                    if (status.Text == "1")
                    {
                        bool kq = Coupon_BUS.UpdateStatusColumnOrderCoupon(orderCouponID, 0);
                        if (kq == false)
                        {
                            flag = 1;
                            LbResult.Text = "Không Thành Công";
                            LbResult.ForeColor = Color.Red;
                            break;
                        }
                       
                    }
                }
            }

            if (flag == 0)
            {
                //LbResult.Text = "Thành Công";
                //LbResult.ForeColor = Color.Blue;
                gvOrderManager.DataSource = Coupon_BUS.GetAllOrderCoupon();
                gvOrderManager.DataBind();
            }
        }

       

        protected void gvOrderManager_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvOrderManager.PageIndex = e.NewPageIndex;
        }
    }
}
